﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-23>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：表卡排序
//********************************************************************************//
using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using System.Text;

namespace Web.Pages.QuShui.DiXiaS.ChaoBiaoGL
{
    public partial class BiaoKaPX :BasePage
    {
        #region 属性

        /// <summary>
        /// 记录当前操作的水表号
        /// </summary>
        private string SHUIBIAOBH
        {
            get
            {
                return Convert.ToString(ViewState["SHUIBIAOBH"]);
            }
            set
            {
                ViewState["SHUIBIAOBH"] = value;
            }
        }

        /// <summary>
        /// 记录当前操作的水表号
        /// </summary>
        private string SHUIBIAOBH2
        {
            get
            {
                return Convert.ToString(ViewState["SHUIBIAOBH2"]);
            }
            set
            {
                ViewState["SHUIBIAOBH2"] = value;
            }
        }
        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }
        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource2
        {
            get
            {
                return ViewState["dtSource2"] as DataTable;
            }
            set
            {
                ViewState["dtSource2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();


        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        /// <summary>
        /// 水表信息
        /// </summary>
        QS_YH_SHUIBIAOXXB _SBXX = new QS_YH_SHUIBIAOXXB();

        /// <summary>
        /// 水表信息bll
        /// </summary>
        QS_YH_SHUIBIAOXXBBLL _SBXXBLL = new QS_YH_SHUIBIAOXXBBLL();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
                LoadData();
                LoadData2();
            }
        }

        /// <summary>
        /// 左侧列表焦点变更事件
        /// </summary>
        protected void gv_FocusedRowChanged(object sender, EventArgs e)
        {
            if (this.gvList.FocusedRowIndex == -1)
            {
                return;
            }
            this.SHUIBIAOBH = Convert.ToString(this.gvList.GetRowValues(this.gvList.FocusedRowIndex, "ID"));
        }
        /// <summary>
        /// 左侧列表焦点变更事件
        /// </summary>
        protected void gv2_FocusedRowChanged(object sender, EventArgs e)
        {
            if (this.gvList2.FocusedRowIndex == -1)
            {
                return;
            }
            this.SHUIBIAOBH2 = Convert.ToString(this.gvList2.GetRowValues(this.gvList2.FocusedRowIndex, "ID"));
        }

        /// <summary>
        /// 上移操作
        /// </summary>
        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.SHUIBIAOBH))
            {
                //获取选中项的位置
                int index = -1;
                for (int i = 0; i < this.dtSource.Rows.Count; i++)
                {
                    if (Convert.ToString(dtSource.Rows[i]["ID"]) == this.SHUIBIAOBH)
                    {
                        index = i;
                        break;
                    }
                }
                if (index > 0)
                {
                    //先复制数据,移出
                    DataRow row = this.dtSource.NewRow();
                    row.ItemArray = new object[] { this.dtSource.Rows[index][0], this.dtSource.Rows[index][1], this.dtSource.Rows[index][2] };
                    this.dtSource.Rows.RemoveAt(index);
                    //再前移一位插入
                    index = index - 1;
                    this.dtSource.Rows.InsertAt(row, index);
                    //重新绑定
                    this.gvList.DataSource = this.dtSource;
                    this.gvList.DataBind();
                    //选中当前操作项
                    PageTools.FocusOpreateRecord(this.dtSource, this.gvList, "index", index.ToString(), true);
                    //LoadData();
                    //this.upList.Update();
                }
            }
        }

        /// <summary>
        /// 下移操作
        /// </summary>
        protected void btnDown_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.SHUIBIAOBH))
            {
                //获取选中项的位置
                int index = -1;
                for (int i = 0; i < this.dtSource.Rows.Count; i++)
                {
                    if (Convert.ToString(dtSource.Rows[i]["ID"]) == this.SHUIBIAOBH)
                    {
                        index = i;
                        break;
                    }
                }
                if (index < this.dtSource.Rows.Count - 1)
                {
                    //先复制数据,移出
                    DataRow row = this.dtSource.NewRow();
                    row.ItemArray = new object[] { this.dtSource.Rows[index][0], this.dtSource.Rows[index][1], this.dtSource.Rows[index][2] };
                    //DataRow row = this.dtSource.Rows[index];
                    this.dtSource.Rows.RemoveAt(index);
                    //再前移一位插入
                    index = index + 1;
                    this.dtSource.Rows.InsertAt(row, index);
                    //重新绑定
                    this.gvList.DataSource = this.dtSource;
                    this.gvList.DataBind();
                    //选中当前操作项
                    PageTools.FocusOpreateRecord(this.dtSource, this.gvList, "index", index.ToString(), true);
                    //LoadData();
                    //this.upList.Update();
                }
            }
        }

        /// <summary>
        /// 保存当前顺序
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveData();
        }

        /// <summary>
        /// 保存当前顺序
        /// </summary>
        protected void btnSave2_Click(object sender, EventArgs e)
        {
            this.SaveData2();
        }
        /// <summary>
        /// 放弃操作
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.LoadData();
            //this.upList.Update();
        }
        /// <summary>
        /// 放弃操作
        /// </summary>
        protected void btnCancel2_Click(object sender, EventArgs e)
        {
            this.LoadData2();
            //this.upList.Update();
        }

        /// <summary>
        /// 加载数据列表
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 加载数据列表
        /// </summary>
        protected void gvList2_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData2();
        }

        /// <summary>
        /// 下拉框改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbCeHao_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }
        /// <summary>
        /// 下拉框改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbCeHao2_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData2();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            LoadCH();
        }

        /// <summary>
        /// 加载册号
        /// </summary>
        private void LoadCH()
        {
            int wtype = BusinessTool.WType;
            string[] arr1 = { "600" };
            string[] arr2 = { "001", "002", "003", "005", "008", "009", "101", "106", "111", "113", "115", "204", "207", "208", "209", "210", "213", "214", "216", "217", "218" };
            string[] arr = wtype == 2 ? arr2 : arr1;
            for (int i = 0; i < arr.Length; i++)
            {
                cmbCeHao.Items.Add(arr[i], arr[i]);
                cmbCeHao2.Items.Add(arr[i], arr[i]);
                cmbCeHao3.Items.Add(arr[i], arr[i]);
            }
            cmbCeHao.SelectedIndex = 0;
            cmbCeHao2.SelectedIndex = 0;
            cmbCeHao3.SelectedIndex = 0;
        }
        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            this.dtSource = _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(string.Format("S_CEHAO='{0}' and i_jlzt=0 ", cmbCeHao.Value.ToString()), "I_XUHAO");
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }
        /// 数据加载
        /// </summary>
        private void LoadData2()
        {
            this.dtSource2 = _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(string.Format("S_CEHAO='{0}' and i_jlzt=0 ", cmbCeHao2.Value.ToString()), "I_XUHAO");
            this.gvList2.DataSource = this.dtSource2;
            this.gvList2.DataBind();
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {

        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            //拼接要更新的记录ID及要更新的序号值
            StringBuilder ids = new StringBuilder("");
            StringBuilder inos = new StringBuilder("");
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                ids.Append(",").Append(this.dtSource.Rows[i]["ID"]);
                inos.Append(",").Append(i);
            }
            if (ids.ToString() != "")
            {
                ids.Append(",");
                inos.Append(",");
                //保存新顺序
                _spHelper.P_QSCB_UPDATEINDEX(ids.ToString(), inos.ToString(), cmbCeHao.Value.ToString());
                PageTools.AlertMsg(this, "保存成功！");
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData2()
        {
            string sbh = txtShuiBiaoH1.Text;
            string ch1 = cmbCeHao2.Text;
            string ch2 = cmbCeHao3.Text;
            _spHelper.P_QSCB_UPDATEINDEX2(ch1, ch2, sbh);
            PageTools.AlertMsg(this, "保存成功！");
            LoadData2();
        }

        #endregion      

       
    }
}